<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="http://117.80.114.220:81/static/lib/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="http://117.80.114.220:81/static/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form" lay-filter="userRoleFrom" sec:authorize="hasAuthority('sys:user:role')">
    <div class="layui-form-item">
        <label class="layui-form-label required">用户名</label>
        <div class="layui-input-block">
            <input type="text" name="userName" value="" class="layui-input" disabled="disabled">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">用户编码</label>
        <div class="layui-input-block">
            <input type="text" name="userCode" value="" class="layui-input" disabled="disabled">
        </div>
    </div>
</div>
<div id="userRole" style="margin: 20px auto;width: 1000px;overflow: hidden;" sec:authorize="hasAuthority('sys:user:role')"></div>
<script src="http://117.80.114.220:81/static/lib/layui/layui.js" charset="utf-8"></script>
<script src="http://117.80.114.220:81/static/lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script src="http://117.80.114.220:81/static/js/my.js"></script>
<script>
    layui.use(['transfer', 'layer', 'util', 'form'], function () {
        var $ = layui.$,
            transfer = layui.transfer,
            layer = layui.layer,
            util = layui.util,
            form = layui.form;

        var userId = getUrlParam("id");
        //加载数据
        myGet("/sys-user/findUser?id=" + userId, null, function (data) {
            console.info(data);
            //给表单赋值
            if (data.code == 0) {
                form.val("userRoleFrom", data.data);
            } else {
                layer.msg(data.msg);
            }
        });

        myGet("/sys-user-role/getRoleAndNRole?id=" + userId, null, function (data) {
            if (data.code == 0) {
                //初始右侧数据
                transfer.render({
                    elem: '#userRole'
                    , title: ['未分配', '已分配']
                    , data: data.data.roles
                    , value: data.data.userRoles
                    , height: 600
                    , onchange: function (data, index) {
                        console.log(data); //得到当前被穿梭的数据
                        console.log(index); //如果数据来自左边，index 为 0，否则为 1
                        //index 为 0 为分配，为 1取消分配
                        myPost("/sys-user-role/save", JSON.stringify({
                            userId: userId,
                            type: index,
                            userRoles: data
                        }), function (data) {
                            if (data.code == 0) {
                                var alert = layer.alert("授权成功", {title: "提示"}, function () {
                                    // 关闭弹出层
                                    layer.close(alert);
                                });
                            } else {
                                layer.msg(data.msg);
                            }
                        });
                    }
                });
            } else {
                layer.msg(data.msg);
            }
        });
    });
</script>
</body>
</html>
